/************************************
*  Copyright (C) 2010 Julien MOTTIN.
*  Contact: julien.mottin@gueydan.eu
*
*  This file is part of Gueydan.
*
*  Gueydan is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*
*  Gueydan is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with Gueydan.  If not, see <http://www.gnu.org/licenses/>.
*
*************************************/
// Local
#include "temperature.h"
#include "util.h"

// QtCore
#include <QString>
#include <QMap>

Temperature::Temperature(const double iTemperature, const TemperatureUnit iUnit)
{
    _Value = iTemperature;
    if (Farhenheit==iUnit)
        _Value = _Value*TCoeffFarhenheit + TShiftFarhenheit;
}

Temperature::Temperature(const Temperature &iT)
{
    _Value = iT._Value;
}

Temperature & Temperature::operator=(const Temperature &iT)
{
    if (this!=&iT)
    {
        _Value = iT._Value;
    }
    return *this;
}

Temperature::~Temperature()
{
}

bool Temperature::operator==(const Temperature &iT) const
{
    return ComparePrecision(_Value,iT._Value,0.05);
}

bool Temperature::operator!=(const Temperature &iT) const
{
    return (!ComparePrecision(_Value,iT._Value,0.05));
}

bool Temperature::operator>=(const Temperature &iT) const
{
    return (_Value>=iT._Value);
}

bool Temperature::operator>(const Temperature &iT) const
{
    return (_Value>iT._Value);
}

bool Temperature::operator<=(const Temperature &iT) const
{
    return (_Value<=iT._Value);
}

bool Temperature::operator<(const Temperature &iT) const
{
    return (_Value<iT._Value);
}

double Temperature::Value(const TemperatureUnit iUnit) const
{
    double oVal = _Value;
    if (Farhenheit==iUnit)
    {
        oVal = (_Value-TShiftFarhenheit)/TCoeffFarhenheit;
    }
    return oVal;
}

// Global Temperature QMap
static QMap<QString,TemperatureUnit> TemperatureUnitMap;
static bool TemperatureUnitMapInit = false;

void Temperature::FromString(const QString &iString, bool *OK)
{
    bool tmpOK = false;

    if (!TemperatureUnitMapInit)
    {
        TemperatureUnitMap["c"] = Celsius;
        TemperatureUnitMap["°c"] = Celsius;
        TemperatureUnitMap["f"] = Farhenheit;
        TemperatureUnitMap["°f"] = Farhenheit;
        TemperatureUnitMapInit = true;
    }

    QString Unit;
    double Val;
    StringToUnit(iString,Val,Unit);

    // Case no Unit
    if (Unit.isEmpty())
    {
        // Just copy value
        _Value = Val;
        tmpOK = true;
    }
    else
    {
        if (TemperatureUnitMap.contains(Unit))
        {
            // Build Temperature with proper unit
            TemperatureUnit zeUnit = TemperatureUnitMap[Unit];
            (*this)=Temperature(Val,zeUnit);
            tmpOK = true;
        }
    }

    if (0!=OK)
        (*OK)=tmpOK;
}
